package com.project.yunshitong.adapter;

import static android.content.ContentValues.TAG;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.project.yunshitong.bean.GoodsInfo;
import com.project.yunshitong.R;

import java.util.List;

public class InventoryAdapter extends BaseAdapter {

    private Context context;
    private List<GoodsInfo> list;
    private ViewHolder viewHolder;
    private Callback callback;

    public InventoryAdapter(Context context, List<GoodsInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_inventory, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        GoodsInfo info = list.get(i);
        viewHolder.tvNo.setText(info.getCode());
        viewHolder.tvProfitNo.setText(info.getCode());
        Log.e(TAG, "getView: info.getStatus()========" + info.getStatus());
        changeState(i, info.getStatus());
        switch (info.getStatus()) {
            case "0":
                viewHolder.tvState.setText("未盘点");
                viewHolder.llNoProfit.setVisibility(View.VISIBLE);
                viewHolder.tvProfitNo.setVisibility(View.GONE);
                viewHolder.tvOperation.setVisibility(View.GONE);
                viewHolder.tvCancel.setVisibility(View.GONE);
                break;
            case "1":
                viewHolder.tvState.setText("盘存");
                viewHolder.llNoProfit.setVisibility(View.VISIBLE);
                viewHolder.tvProfitNo.setVisibility(View.GONE);
                viewHolder.tvOperation.setVisibility(View.GONE);
                viewHolder.tvCancel.setVisibility(View.GONE);
                break;
            case "2":
                viewHolder.tvState.setText("盘盈");
                viewHolder.llNoProfit.setVisibility(View.GONE);
                viewHolder.tvProfitNo.setVisibility(View.VISIBLE);
                viewHolder.tvOperation.setVisibility(View.GONE);
                viewHolder.tvCancel.setVisibility(View.GONE);
                break;
            case "3":
                viewHolder.tvState.setText("盘盈");
                viewHolder.llNoProfit.setVisibility(View.VISIBLE);
                viewHolder.tvProfitNo.setVisibility(View.GONE);
                viewHolder.tvOperation.setVisibility(View.GONE);
                viewHolder.tvCancel.setVisibility(View.GONE);
                break;
            case "4":
                viewHolder.tvState.setText("待确认");
                viewHolder.llNoProfit.setVisibility(View.VISIBLE);
                viewHolder.tvProfitNo.setVisibility(View.GONE);
                viewHolder.tvOperation.setVisibility(View.VISIBLE);
                viewHolder.tvCancel.setVisibility(View.VISIBLE);
                break;
        }
//        viewHolder.tvVer.setText(info.getBomid());
//        viewHolder.tvDepId.setText(info.getDeptid());
        viewHolder.tvUser.setText(info.getUser());
        viewHolder.tvUserDepart.setText(info.getDepartment());
        viewHolder.tvModel.setText(info.getModel());
        viewHolder.tvName.setText(info.getName());
//        viewHolder.tvSerialNumber.setText(info.getNo());
        viewHolder.tvAddress.setText(info.getAddress());
//        viewHolder.tvUnit.setText(info.getUnit());
//        viewHolder.tvDepart.setText(info.getInDepart());
        viewHolder.tvOperation.setOnClickListener(view1 -> callback.click(i));
        viewHolder.tvCancel.setOnClickListener(view1 -> callback.cancel(i));
        viewHolder.tvUserDepart.setOnClickListener(view1 -> {
            if ("0".equals(info.getStatus()) || "1".equals(info.getStatus()) || "3".equals(info.getStatus())) {
                callback.change(i);
            }
        });
        return view;
    }

    private void changeState(int index, String state) {
        viewHolder.tvState.setOnClickListener(v -> {
            switch (state) {
                case "0":
                case "1":
                case "3":
                    callback.changeState(index, state);
                    break;
                case "2":
                case "4":
                    break;
            }
        });
    }

    public void setClick(Callback callback) {
        this.callback = callback;
    }

    public interface Callback {
        void click(int index);

        void changeState(int index, String state);

        void cancel(int index);

        void change(int index);//修改部门
    }

    private class ViewHolder {
        private TextView tvOperation;
        private TextView tvCancel;
        private TextView tvProfitNo;
        private TextView tvNo;
        private TextView tvState;
        private TextView tvUser;
        private TextView tvUserDepart;
        private TextView tvModel;
        private TextView tvName;
        private TextView tvAddress;
        private LinearLayout llNoProfit;

        public ViewHolder(View view) {
            tvOperation = view.findViewById(R.id.tvOperation);
            tvCancel = view.findViewById(R.id.tvCancel);
            tvProfitNo = view.findViewById(R.id.tvProfitNo);
            tvNo = view.findViewById(R.id.tvNo);
            tvState = view.findViewById(R.id.tvState);
            tvUser = view.findViewById(R.id.tvUser);
            tvUserDepart = view.findViewById(R.id.tvUserDepart);
            tvModel = view.findViewById(R.id.tvModel);
            tvName = view.findViewById(R.id.tvName);
            tvAddress = view.findViewById(R.id.tvAddress);
            llNoProfit = view.findViewById(R.id.llNoProfit);
        }
    }
}
